package servlet;

import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Database.AppointmentDB;
import Database.StaffAppointmentDB;
import Model.Appointment;
import Model.Approval;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * Servlet implementation class ApptPrint
 */

public class ApprovedTablePrint extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ApprovedTablePrint() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/pdf");
		Document myDocument = new Document(PageSize.A4, 36, 36, 30, 72);

		Approval app = StaffAppointmentDB.getNameAndId(request
				.getParameter("md5"));

		ArrayList<Approval> AppList = StaffAppointmentDB
				.getStaffOfAppointment2(app.getAppointment());

		Appointment appt = AppointmentDB.getAppointmentbyID(app
				.getAppointment());
		String date = StaffAppointmentDB.getDate(request.getParameter("md5"));

		try {
			// Writes particulars into PDF file
			PdfWriter.getInstance(myDocument, response.getOutputStream());

			// myDocument.setMargins(36, 10, 108, 180);
			myDocument.setMarginMirroring(true);

			myDocument.open();
			Font fontbold = new Font();
			fontbold.setStyle(Font.BOLD);

			try{
				String imageUrl = "http://www.nyp.edu.sg/nyp/slot/u100/style/homepage/logo.jpg";
			Image image = 
				Image.getInstance(new URL(imageUrl));
			myDocument.add(image);
			}
			catch(Exception ex){}

			Paragraph paragraph = new Paragraph("APPOINTMENT", fontbold);
			paragraph.setAlignment(Element.ALIGN_CENTER);
			myDocument.add(paragraph); // Heading

			myDocument
					.add(new Paragraph(
							"______________________________________________________________________________")); // Divider
																												// line

			myDocument.add(new Paragraph("     ")); // spacing

			Chunk App_name = new Chunk("Role : " + appt.getName(), fontbold); // Appointment
																	// Name
			App_name.setUnderline(1f, -2f);
			myDocument.add(App_name);

			myDocument.add(new Paragraph("     ")); // spacing

			String name = "";
			for (int i = 0; i < AppList.size(); i++) {
				name = AppList.get(i).getName();
				myDocument.add(new Paragraph(name)); // List of names
			}
			myDocument.add(new Paragraph("     ")); // spacing

			Chunk Roles = new Chunk("Roles & Responsibilities", fontbold);
			Roles.setUnderline(1f, -2f); // Roles & Responsibilities
											// heading
			myDocument.add(Roles);

			myDocument.add(new Paragraph("     ")); // spacing

			myDocument.add(new Paragraph(appt.getResponsibilities())); // list
																		// of
																		// Roles
																		// &
			// Responsibilities

			myDocument.add(new Paragraph("     ")); // spacing
			myDocument.add(new Paragraph("     ")); // spacing

			myDocument.add(new Paragraph(
					"Approved by:___________________________________"));

			myDocument
					.add(new Paragraph(
							"                      Director, School Of Information Technology"));

			myDocument.add(new Paragraph("     ")); // spacing

			myDocument
					.add(new Paragraph("Effective Date: " + formatdate(date))); // spacing

			myDocument.add(new Paragraph("     ")); // spacing
			myDocument.add(new Paragraph("     ")); // spacing

			Font f = new Font();
			f.setColor(BaseColor.BLUE);
			f.setStyle(Font.ITALIC);
			myDocument.add(new Paragraph("This is an electronic copy. No signature is required.",f));
			/*
			 *This function is to generate date for the document but is not needed now.			
			DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
			Date getDate = new Date();
			String currentDate = dateFormat.format(getDate);
			myDocument.add(new Paragraph("This document is generated on " + currentDate,f));
*/
		} catch (DocumentException de) {
			System.err.println(de.getMessage());
		} catch (IOException ioe) {
			System.err.println(ioe.getMessage());
		}

		myDocument.close();

		// End of Appointment letter codes
	}

	private String formatdate(String date) {
		String result = date.split(" ")[0] + " " + date.split(" ")[1].charAt(0)
				+ date.split(" ")[1].substring(1).toLowerCase() + " "
				+ date.split(" ")[2];
		return result;
	}
}
